#include <gtk/gtk.h>
#include <zmq.h>
#include <stdlib.h>
#include <string.h>

#define BOX_SPACING 5


static GtkWidget *window;
static GtkWidget *label, *button, *textEntry, *vbox, *hbox;
static void *context, *sender;

static gboolean deleteEvent(GtkWidget *widget, gpointer data)
{
	return FALSE;
}

static void destroyEvent (GtkWidget *widget, gpointer data)
{
	zmq_close(sender);
	zmq_term(context);
	gtk_main_quit();
}

static void buttonClicked (GtkObject *widget, gpointer data)
{
	zmq_msg_t request, reply;
	int replyLen;
	char *answ, *text = gtk_entry_get_text((struct GtkEntry*)textEntry);
	

	/*Send a message to server*/
	zmq_msg_init_size(&request, strlen(text));
	memcpy(zmq_msg_data(&request), text, strlen(text));
	zmq_send(sender, &request, 0);
	zmq_msg_close(&request);

	/*Recieve a reply from server*/
	zmq_msg_init(&reply);
	zmq_recv(sender, &reply, 0);
	replyLen = zmq_msg_size(&reply);
	answ = malloc (sizeof(char*)*(replyLen + 1));
	memcpy(answ, zmq_msg_data(&reply), replyLen);
	zmq_msg_close(&reply);
	answ[replyLen] = '\0';

	/*Change label text*/
	gtk_label_set_text((struct GtkLabel*)(label), answ);
}

int main(int argc, char ** argv)
{

	gtk_init (&argc, &argv);
	context = zmq_init(1);

	/*Socket*/
	sender = zmq_socket(context, ZMQ_REQ);
	zmq_connect(sender, "tcp://localhost:5557");
	
	/*Window*/
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (window, "delete_event", G_CALLBACK(deleteEvent), NULL);
	g_signal_connect (window, "destroy_event", G_CALLBACK(destroyEvent), NULL);

	/*Label (main text area)*/
	label = gtk_label_new("Some text");

	/*Text Entrie*/
	textEntry = gtk_entry_new();
	
	/*Button*/
	button = gtk_button_new_with_label("OK");
	g_signal_connect(button, "clicked", G_CALLBACK(buttonClicked), NULL);

	/*Widgets packing*/
	vbox = gtk_vbox_new (FALSE, BOX_SPACING);
	hbox = gtk_hbox_new (FALSE, BOX_SPACING);
	gtk_box_pack_start(GTK_BOX(hbox), textEntry, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
	gtk_container_add(GTK_CONTAINER(window), vbox);


	/*Finaly let us show widgets*/
	gtk_widget_show  (label);
	gtk_widget_show  (button);
	gtk_widget_show  (textEntry);
	gtk_widget_show  (hbox);
	gtk_widget_show  (vbox);
	gtk_widget_show  (window);
	gtk_main ();

	return 0;
}
